import pygame
from contants import*
from module import *
import time


class Game:
    def __init__(self):
        #设置窗口大小，获取surface(渲染)
        self.screen = pygame.display.set_mode(size=(screen_width,screen_height))

        #设置标题
        pygame.display.set_caption("贪吃蛇ikun版v1.0")
        #设置图标
        icon = pygame.image.load("res/food.gif")
        pygame.display.set_icon(icon)
        #加载背景图
        bg_image = pygame.image.load("res/bjb.png")
        self.bg_image = pygame.transform.scale(bg_image,(screen_width,screen_height))
        #结束图
        over_image = pygame.image.load("res/body.jpg")
        self.over_image = pygame.transform.scale(over_image,(10*block_size,10*block_size))
        
        
        
    def start(self):
        #clock
        clock = pygame.time.Clock()
        snake = Snake()
        food = Food(Food.gen_foof_position(snake))
        is_game_over = False

        while True:
            #处理事件，获取用户的输入事件
            event_list = pygame.event.get()
            #解析处理所有事件
            #even.type 用户输入的事件  event.key 保存按下或抬起的按键
            for event in event_list:
                if event.type == pygame.QUIT:
                    print("点击了关闭")
                    pygame.display.quit()
                    exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        print("向上")
                        snake.update_direction(pygame.K_UP)
                    elif event.key == pygame.K_RIGHT:
                        print("向右")
                        snake.update_direction(pygame.K_RIGHT)
                    elif event.key == pygame.K_DOWN:
                        print("向下")
                        snake.update_direction(pygame.K_DOWN)
                    elif event.key == pygame.K_LEFT:
                        print("向左")
                        snake.update_direction(pygame.K_LEFT)
                    elif event.key == pygame.K_ESCAPE:
                        print("关闭")
                        exit()
            
            if not is_game_over:
                #处理游戏逻辑
                snake.move()
                #遇到食物，吃掉食物
                snake_head = snake.snake_body[0]
                if snake_head == food.node:
                    #吃掉食物，刷新食物
                    food = Food(Food.gen_foof_position(snake))
                    pygame.mixer.music.load("res/ji.mp3")
                    pygame.mixer.music.play()
                    #蛇身体边长
                    snake.grow()
                #碰到自己
                if snake_head in snake.snake_body[1:]:
                    pygame.mixer.music.load("res/shuangjie_ji.mp3")
                    pygame.mixer.music.play(0)
                    
                    is_game_over = True
                    
                     
            #渲染界面
            self.screen.blit(self.bg_image,(0,0)) #背景图
            for y in range(0,screen_height,block_size):
                start = (0,y)
                end = (screen_width,y)
                pygame.draw.line(self.screen,color_gary,start,end)  #绘制横线
            for x in range(0,screen_width,block_size):
                start = (x,0)
                end = (x,screen_height)
                pygame.draw.line(self.screen,color_gary,start,end)  #绘制竖线
        
            #绘制蛇身
            snake.draw(self.screen)
        
            #绘制食物
            food.draw(self.screen)
            
            #绘制得分与fps
            fps = clock.get_fps()
            self.show_text("FPS:{:.2f}".format(fps),20,screen_width - 150,50)
            
            #根据游戏是否结束渲染文字
            if is_game_over:
                self.screen.blit(self.over_image,(200,10))
            
    
            pygame.display.flip() #执行最终渲染
            #设定FPS
            clock.tick(8)
            
    def show_text(self,text,font_size,x,y):
        font1 = pygame.font.SysFont("SimHei",font_size)
        text = font1.render(text,True,color_blue)
        self.screen.blit(text,(x,y))
        